<template>
  <div class="page-container">
    <!-- 数据源状态 -->
    <a-card class="data-source-card">
      <template #title>
        <div class="card-header">
          <span>数据源状态</span>
          <a-button type="link">查看全部</a-button>
        </div>
      </template>
      <a-list :dataSource="dataSources" :grid="{ gutter: 16, column: 2 }">
        <template #renderItem="{ item }">
          <a-list-item>
            <a-card :bodyStyle="{ padding: '12px' }">
              <div class="data-source-item">
                <component :is="item.icon" class="data-source-icon" />
                <div class="data-source-info">
                  <div class="data-source-name">{{ item.name }}</div>
                  <div class="data-source-type">{{ item.type }}</div>
                  <div class="data-source-time">最后同步: {{ item.lastSync }}</div>
                </div>
                <a-tag :color="item.status === '正常' ? 'success' : 'warning'">
                  {{ item.status }}
                </a-tag>
              </div>
            </a-card>
          </a-list-item>
        </template>
      </a-list>
    </a-card>

    <!-- 存储状态 -->
    <a-card class="storage-card">
      <template #title>存储状态</template>
      <div class="storage-content">
        <div class="storage-chart">
          <v-chart class="chart" :option="storageChartOption" autoresize />
        </div>
        <div class="storage-info">
          <div class="storage-item">
            <div class="storage-label">实时数据</div>
            <div class="storage-value">{{ storageStatus.realtime.used }}/{{ storageStatus.realtime.total }}GB</div>
            <a-progress :percent="storageStatus.realtime.percent" :status="storageStatus.realtime.percent > 80 ? 'exception' : 'normal'" />
          </div>
          <div class="storage-item">
            <div class="storage-label">历史数据</div>
            <div class="storage-value">{{ storageStatus.historical.used }}/{{ storageStatus.historical.total }}GB</div>
            <a-progress :percent="storageStatus.historical.percent" :status="storageStatus.historical.percent > 80 ? 'exception' : 'normal'" />
          </div>
        </div>
      </div>
    </a-card>

    <!-- 备份状态 -->
    <a-card class="backup-card">
      <template #title>备份状态</template>
      <div class="backup-content">
        <div class="backup-item">
          <div class="backup-info">
            <div class="backup-label">自动备份</div>
            <div class="backup-time">上次备份: {{ backupStatus.lastBackup }}</div>
          </div>
          <a-switch v-model:checked="backupStatus.autoBackup" />
        </div>
        <div class="backup-item">
          <div class="backup-info">
            <div class="backup-label">备份频率</div>
            <div class="backup-time">每日 00:00</div>
          </div>
          <a-button type="link">修改</a-button>
        </div>
      </div>
    </a-card>

    <!-- 安全状态 -->
    <a-card class="security-card">
      <template #title>安全状态</template>
      <div class="security-content">
        <div class="security-item">
          <div class="security-info">
            <div class="security-label">数据加密</div>
            <div class="security-desc">AES-256 加密</div>
          </div>
          <a-tag color="success">已启用</a-tag>
        </div>
        <div class="security-item">
          <div class="security-info">
            <div class="security-label">访问控制</div>
            <div class="security-desc">基于角色的访问控制</div>
          </div>
          <a-button type="link">查看日志</a-button>
        </div>
      </div>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { PieChart } from 'echarts/charts'
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent
} from 'echarts/components'
import VChart from 'vue-echarts'
import {
  DatabaseOutlined,
  CloudServerOutlined,
  ApiOutlined,
  FileTextOutlined
} from '@ant-design/icons-vue'

// 注册 ECharts 组件
use([
  CanvasRenderer,
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent
])

// 数据源列表
const dataSources = ref([
  {
    name: 'MES系统',
    type: '实时数据',
    status: '正常',
    lastSync: '2024-02-20 10:30',
    icon: DatabaseOutlined
  },
  {
    name: 'ERP系统',
    type: '业务数据',
    status: '正常',
    lastSync: '2024-02-20 09:15',
    icon: CloudServerOutlined
  },
  {
    name: '设备数据',
    type: '实时数据',
    status: '警告',
    lastSync: '2024-02-20 08:45',
    icon: ApiOutlined
  },
  {
    name: '质量数据',
    type: '历史数据',
    status: '正常',
    lastSync: '2024-02-20 10:00',
    icon: FileTextOutlined
  }
])

// 存储状态
const storageStatus = ref({
  realtime: {
    used: 45,
    total: 100,
    percent: 45
  },
  historical: {
    used: 320,
    total: 500,
    percent: 64
  }
})

// 存储图表配置
const storageChartOption = ref({
  tooltip: {
    trigger: 'item',
    formatter: '{b}: {c}GB ({d}%)'
  },
  legend: {
    orient: 'vertical',
    right: 10,
    top: 'center'
  },
  series: [
    {
      name: '存储使用',
      type: 'pie',
      radius: ['50%', '70%'],
      avoidLabelOverlap: false,
      label: {
        show: false
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '14',
          fontWeight: 'bold'
        }
      },
      labelLine: {
        show: false
      },
      data: [
        { value: 45, name: '实时数据' },
        { value: 320, name: '历史数据' },
        { value: 135, name: '可用空间' }
      ]
    }
  ]
})

// 备份状态
const backupStatus = ref({
  autoBackup: true,
  lastBackup: '2024-02-20 00:00'
})
</script>

<style lang="less" scoped>
.data-source-card,
.storage-card,
.backup-card,
.security-card {
  margin-bottom: 16px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.data-source-item {
  display: flex;
  align-items: center;
  gap: 12px;
}

.data-source-icon {
  font-size: 24px;
  color: @primary-color;
}

.data-source-info {
  flex: 1;
}

.data-source-name {
  font-weight: bold;
  margin-bottom: 4px;
}

.data-source-type,
.data-source-time {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
}

.storage-content {
  display: flex;
  gap: 24px;
}

.storage-chart {
  flex: 1;
  height: 300px;
}

.storage-info {
  width: 300px;
}

.storage-item {
  margin-bottom: 16px;
}

.storage-label {
  margin-bottom: 8px;
}

.storage-value {
  margin-bottom: 4px;
}

.backup-content,
.security-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.backup-item,
.security-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.backup-info,
.security-info {
  flex: 1;
}

.backup-label,
.security-label {
  font-weight: bold;
  margin-bottom: 4px;
}

.backup-time,
.security-desc {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
}

.chart {
  height: 100%;
}
</style> 